package cn.aiyuan.controller;

import cn.aiyuan.pojo.WebSocket;
import cn.aiyuan.service.WebSocketServer;
import com.alibaba.fastjson.JSON;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import javax.websocket.Session;
import java.util.Map;


/**
 * <websocket测试用MVC控制器>
 * <功能详细描述>
 * @version 2018-07-09 22:53
 * @see [相关类/方法] (可选)
 **/
@Controller
@RequestMapping("/websocket")
public class WebSocketController {

    @Resource
    private WebSocketServer webSocketServer;

    @RequestMapping(value = "socket",method = RequestMethod.GET)
    public String toWebSocket(HttpSession session, Model model){
        //model.addAttribute("address","/javax/websocket");
        return "index";
    }

    @GetMapping("sendMsg/{id}/{msg}")
    @ResponseBody
    public String sendMsg(@PathVariable Integer id, @PathVariable String msg){
        Map<String, WebSocket> onlineUser = webSocketServer.getOnlineUser();
        WebSocket webSocketBean = onlineUser.get(id.toString());
        Session session = webSocketBean.getSession();
        webSocketServer.sendMessage(session,msg);
        return "发送id："+id.toString() + "---消息为：" + msg;
    }
}
